package com.dingpeikeji.quickdeer.modules.wx.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dingpeikeji.quickdeer.modules.wx.pojo.AccessToken;

public class TokenThread implements Runnable{
	
	private static Logger log = LoggerFactory.getLogger(TokenThread.class);     
    // 第三方用户唯一凭证    
    //public static String appid = "wx6e0856e26f30fe47";    
    public static String appid = "wx70cbd1f125e9b16e";    
    // 第三方用户唯一凭证密钥    
    //public static String appsecret = "5317df643e43d90dee096896341b2915";    
    public static String appsecret = "9db656de9f69c978ccbf4220be164dc3";    
    //保存ACCESS_TOKEN到内存
    public static AccessToken accessToken = null;    
    
    public void run() {  
        while (true) {  
            try {  
                accessToken = WeixinUtil.getAccessToken(appid, appsecret);  
                if(null != accessToken) {  
                    log.info("获取access_token成功，有效时长{}秒 token:{}", accessToken.getExpiresIn(), accessToken.getToken());  
                    // 休眠7000秒  
                    Thread.sleep((accessToken.getExpiresIn() - 200) * 1000);  
                }else{  
                    // 如果access_token为null，60秒后再获取  
                    Thread.sleep(60 * 1000);  
                }  
            } catch (InterruptedException e) {  
                try{  
                    Thread.sleep(60 * 1000);  
                } catch (InterruptedException e1) {  
                    log.error("{}", e1);  
                }  
                log.error("{}", e);  
            }  
        }  
    }     
}
